#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import cv2
import numpy as np
import pytesseract
import time
import sys
import re
import os

from matplotlib import pyplot as plt

from log import get_logger

logger = get_logger(__name__)


def nothing(x):
    pass


def show_img(img):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    img = img[90:120, 1170:1207]
    # 初步清理颜色
    FILTER_MIN = np.array([0, 0, 135], np.uint8)
    FILTER_MAX = np.array([255, 93, 255], np.uint8)
    frame_threshed = cv2.inRange(img, FILTER_MIN, FILTER_MAX)
    img = cv2.bitwise_or(img, img, mask=frame_threshed)
    # 清理金色
    FILTER_MIN = np.array([20, 0, 0], np.uint8)
    FILTER_MAX = np.array([147, 255, 255], np.uint8)
    frame_threshed = cv2.bitwise_not(cv2.inRange(img, FILTER_MIN, FILTER_MAX))
    img = cv2.bitwise_or(img, img, mask=frame_threshed)
    img = cv2.cvtColor(img, cv2.COLOR_HSV2RGB)

    # img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    # cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
    # cv2.createTrackbar('R1', 'image', 20, 255, nothing)
    # cv2.createTrackbar('R2', 'image', 147, 255, nothing)
    # cv2.createTrackbar('G1', 'image', 0, 255, nothing)
    # cv2.createTrackbar('G2', 'image', 255, 255, nothing)
    # cv2.createTrackbar('B1', 'image', 0, 255, nothing)
    # cv2.createTrackbar('B2', 'image', 255, 255, nothing)
    # switch = '0:OFF\n1:ON'
    # cv2.createTrackbar(switch, 'image', 0, 1, nothing)
    #
    # img_crop = img
    # while (1):
    #     size = img.shape
    #     cv2.imshow('image', img_crop)
    #     k = cv2.waitKey(1) & 0xFF
    #     if k == 113:
    #         break
    #     r1 = cv2.getTrackbarPos('R1', 'image')
    #     r2 = cv2.getTrackbarPos('R2', 'image')
    #     g1 = cv2.getTrackbarPos('G1', 'image')
    #     g2 = cv2.getTrackbarPos('G2', 'image')
    #     b1 = cv2.getTrackbarPos('B1', 'image')
    #     b2 = cv2.getTrackbarPos('B2', 'image')
    #     FILTER_MIN = np.array([r1, g1, b1], np.uint8)
    #     FILTER_MAX = np.array([r2, g2, b2], np.uint8)
    #     # FILTER_MIN = np.array([r], np.uint8)
    #     # FILTER_MAX = np.array([255], np.uint8)
    #     frame_threshed = cv2.bitwise_not(cv2.inRange(img, FILTER_MIN, FILTER_MAX))
    #     # frame_threshed = cv2.inRange(img, FILTER_MIN, FILTER_MAX)
    #     img_crop = cv2.bitwise_or(img, img, mask=frame_threshed)
    #     img_crop = cv2.cvtColor(img_crop, cv2.COLOR_HSV2BGR)
    #     s = cv2.getTrackbarPos(switch, 'image')
    #     if s == 1:
    #         logger.debug(pytesseract.image_to_string(img_crop, lang='chi_sim'))
    #     time.sleep(0.1)
    #
    # cv2.destroyAllWindows()

    plt.imshow(img)
    mng = plt.get_current_fig_manager()
    mng.resize(*mng.window.maxsize())
    plt.show()

    res = pytesseract.image_to_string(img, lang='eng', config='--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789')
    print(res)
    if not res.isdigit():
        return False, -1
    else:
        return True, int(res)


def digit_recongnition():
    img = cv2.imread('/home/liusen/IdeaWorkspace/wechat-jump/mijing3/01.png')

    # cv2.namedWindow('image', cv2.WINDOW_AUTOSIZE)
    # cv2.createTrackbar('R1', 'image', 45, 360, nothing)
    # img_crop = img
    # while (1):
    #     cv2.imshow('image', img_crop)
    #     k = cv2.waitKey(1) & 0xFF
    #     if k == 113:
    #         break
    #     r1 = cv2.getTrackbarPos('R1', 'image')
    #     rows, cols, _ = img.shape
    #     M = cv2.getRotationMatrix2D((cols / 2, rows / 2), r1, 1)
    #     img_crop = cv2.warpAffine(img, M, (cols, rows))
    #
    #     time.sleep(0.1)
    #
    # cv2.destroyAllWindows()
    # sys.exit()

    rows, cols, _ = img.shape

    M = cv2.getRotationMatrix2D((cols / 2, rows / 2), 352, 1)
    img = cv2.warpAffine(img, M, (cols, rows))

    plt.imshow(img)
    mng = plt.get_current_fig_manager()
    mng.resize(*mng.window.maxsize())
    plt.show()


if __name__ == '__main__':
    img = cv2.imread('/home/liusen/IdeaWorkspace/wechat-jump/mijing/201809-scene-伊芙琳教堂.png')

    show_img(img)
    # files = os.listdir('/home/liusen/IdeaWorkspace/wechat-jump/mijing')
    # # a = re.match('201809-scene-[^-]*.png', '201809-scene-伊芙琳教堂.png', flags=0)
    # a = list(filter(lambda x: re.match('201809-scene-[^-]*.png', str(x), flags=0) is not None, files))
    # print(a)
    # for f in files:
    #     str(f))
